package com.cxc.news.service.Impl;

import com.cxc.news.dao.TypeDao;
import com.cxc.news.pojo.Type;
import com.cxc.news.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeDao typeDao;

    @Transactional
    @Override
    public boolean saveType(Type type) {
        typeDao.saveType(type);
        return true;
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        return typeDao.getType(id);
    }

    @Transactional
    @Override
    public PageInfo<Type> listType(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Type> types = typeDao.getTypeList();
        PageInfo<Type> pageInfo = new PageInfo<>(types);

        return pageInfo;
    }



    @Transactional
    @Override
    public boolean updateType(Long id, Type type) {
        Type t = typeDao.getType(id);
        typeDao.updateType(id,type.getName());
        return true;
    }

    @Transactional
    @Override
    public void deleteType(Long id) {
        typeDao.deleteType(id);
    }

    @Transactional
    @Override
    public List<Type> getTypeList() {
        return typeDao.getTypeList();
    }

    @Override
    public Type getTypeByName(String name) {
        return typeDao.getTypeByName(name);
    }

    @Override
    public List<Type> getTypeTop(Integer size) {
        return typeDao.getTypeTop(size);
    }
}
